+function ($) {
    "use strict";
    var dynamicitem, defaults = {
        list: [],
        userId: localStorage.getItem("userId") || "",
        btns: {
            deletebtn: true,
            reportbtn: true,
            followbtn: false
        },
        detail: false,
        showtopic: true,
        onLikeSuccess: undefined,
        onCollectSuccess: undefined,
        onFollowSuccess: undefined,
        onDeleteSuccess: undefined,
        onEvaluateSuccess: undefined,
        tpl: '{{each list item index}}\
        <div class="dynamicitem page-bd-15" data-item="{{item}}">\
        <div class="lobster_dynamic_detail">\
        <div class="weui-cell" style="padding:15px 0;">\
            <div class="weui-cell__hd"><img class="userImg" src="{{item.UserImg}}" alt="" style="width:40px;height:40px;border-radius: 50%;margin-right:5px;display:block;"></div>\
            <div class="weui-cell__bd">\
                <p>{{item.NickName}}</p>\
            </div>\
            <div class="weui-cell__ft">\
                {{if(btns.followbtn&&item.UserId!=userId)}}\
                {{if(item.IsConcern==1)}}\
                <label class="label f-white bg-green lobster_dynamic_follow">已关注</label>\
                {{else}}\
                <label class="label f-gray b-green lobster_dynamic_follow">关注</label>\
                {{/if}}\
                {{/if}}\
            </div>\
        </div>\
        <h4 class="weui-media-box__title">{{item.Title}}</h4>\
        {{if(detail)}}\
        <p style="margin-bottom:10px;color: #999999;font-size: 13px;line-height: 1.2;">{{item.Content}}</p>\
        {{else}}\
        <p class="weui-media-box__desc" style="margin-bottom:10px;">{{item.Content}}</p>\
        {{/if}}\
        {{if(item.DynamicType==1)}}\
            <div class="weui-uploader">\
            <div class="weui-uploader__bd">\
            <ul class="weui-uploader__files" id="imglist{{item.DynamicId}}">\
                {{each item.Attachlist item2}}\
                <li class="weui-uploader__file" src="{{item2}}" style="background-image:url({{item2}})"></li>\
                {{/each}}\
            </ul>\
            </div>\
            </div>\
        {{else}}\
            {{if(detail)}}\
            <video controls width="100%" height="300px" webkit-playsinline="true" playsinline="true" src="{{item.Attachlist[0]}}"></video>\
            {{else}}\
            <div style="position:relative;">\
            <image class="video" style="width:100%;height:300px;display:block;object-fit:cover;" src="{{item.Attachlist[0]}}?x-oss-process=video/snapshot,t_0,f_jpg"></image>\
            <div data-item="{{item}}" style="position:absolute;top: 50%;\
            left: 50%;\
            transform: translate(-50%, -50%);width: 40px;\
            height: 40px;">\
                <image style="width: 40px;height: 40px;" src="/weuiapp/3079/images/play2.png"></image>\
            </div>\
            </div>\
            {{/if}}\
        {{/if}}\
        </div>\
                <div class="weui-cells" >\
                    {{if(showtopic&&item.TopicName)}}\
                    <div style="padding:5px 0" class="weui-cell lobster_dynamic_topic">\
                    <div class="weui-cell__hd"><img src="/weuiapp/3079/images/topic.png" alt="" style="width:20px;margin-right:5px;display:block"></div>\
                    <div class="weui-cell__bd">\
                         <p>{{item.TopicName}}</p>\
                    </div>\
                    <div class="weui-cell__ft"></div>\
                    </div>\
                    {{/if}}\
                    {{if(item.Location)}}\
                    <div style="padding:5px 0" class="weui-cell">\
                    <div class="weui-cell__hd"><img src="/weuiapp/3079/images/location.png" alt="" style="width:20px;margin-right:5px;display:block"></div>\
                    <div class="weui-cell__bd">\
                         <p>{{item.Location}}</p>\
                    </div>\
                    <div class="weui-cell__ft"></div>\
                    </div>\
                    {{/if}}\
                </div>\
        <div style="display: flex;justify-content: space-between;">\
            <div>\
                {{if(item.IsLike==1)}}\
                <span data-islike={{item.IsLike}} class="icon icon-38 lobster_dynamic_like"></span><span style="padding-left: 8px;">{{item.LikesCount}}</span>\
                {{else}}\
                <span data-islike={{item.IsLike}} class="icon icon-65 lobster_dynamic_like"></span><span style="padding-left: 8px;">{{item.LikesCount}}</span>\
                {{/if}}\
                <span class="icon icon-79 lobster_dynamic_evaluate"></span><span style="padding-left: 8px;">{{item.EvaluateCount}}</span>\
                {{if(item.IsCollection==1)}}\
                <span data-iscollection={{item.IsCollection}} class="icon icon-48 lobster_dynamic_collect"></span>\
                {{else}}\
                <span data-iscollection={{item.IsCollection}} class="icon icon-49 lobster_dynamic_collect"></span>\
                {{/if}}\
            </div>\
            <div>\
                {{if(btns.reportbtn&&item.UserId!=userId)}}\
                <span class="icon icon-40 lobster_dynamic_report"></span>\
                {{/if}}\
                {{if(btns.deletebtn&&item.UserId==userId)}}\
                <span class="icon icon-26 lobster_dynamic_delete"></span>\
                {{/if}}\
            </div>\
        </div>\
        <p class="weui-media-box__desc" style="margin-bottom:10px;">{{(item.CreateTime||"").substring(0,19)}}</p>\
        <div style="height:5px;background-color:#f5f5f5;"></div>\
        </div>\
        {{/each}}',
    }
    function convertdata(templist) {
        templist.forEach(a => {
            a.Attachlist = (a.Attachs || "").split(',').filter(b => {
                return (b || "") != "";
            });
            a.Durationlist = (a.Durations || "").split(',').filter(b => {
                return (b || "") != "";
            }).map(n => parseInt(n));
            a.Heightlist = (a.Heights || "").split(',').filter(b => {
                return (b || "") != "";
            });
            a.Widthlist = (a.Widths || "").split(',').filter(b => {
                return (b || "") != "";
            });
            a.EvaluateCount = convertcount(a.EvaluateCount);
            a.LikesCount = convertcount(a.LikesCount);
            a.IsLike = a.IsLike || 0;
            a.IsCollection = a.IsCollection || 0;
            a.IsReport = a.IsReport || 0;
        })
    }
    function convertcount(count) {
        var count = count || 0;
        if (count >= 10000) {
            count = (count / 10000).toFixed(1) + 'w';
        }
        return count;
    }
    $.fn.lobsterdynamicitem = function (params) {
        convertdata(params.list);
        return this.each(function () {
            params = $.extend({}, defaults, params);
            dynamicitem = $(this);
            var html = template.render(params.tpl, params);
            dynamicitem.append(html);
            //点击跳转详情
            $(".lobster_dynamic_detail").click(function (event) {
                if (!params.detail) {
                    var item = $(this).parents(".dynamicitem").data("item");
                    location.href = "/weuiapp/3079/dynamic/detail.html?dynamicId=" + item.DynamicId;
                }
            });
            //点击头像
            $(".userImg").click(function (event) {
                event.stopPropagation();
                var item = $(this).parents(".dynamicitem").data("item");
                location.href = "/weuiapp/3079/blogger/index.html?bloggerId=" + item.BloggerId;
            });
            //关注 取消关注
            $(".lobster_dynamic_follow").click(function (event) {
                event.stopPropagation();
                var _this = $(this);
                var txt = _this.text();
                var item = _this.parents(".dynamicitem").data("item");
                lobsterh5.POST("/lowcode/APP20211203093514696/name/?fun=Dynamic.FollowBlogger", {
                    BloggerId: item.BloggerId,
                    IsConcern: txt == "已关注" ? 0 : 1,
                    ConcernUserId: item.UserId,
                    UserId: params.userId
                }).then(res => {
                    $.toast(txt == "已关注" ? '取消成功' : '关注成功', 'text');
                    $(".dynamicitem").forEach(a => {
                        var dynamicitem = $(a).data("item");
                        var dynamicfollow = $(a).find(".follow");
                        if (dynamicitem.BloggerId == item.BloggerId) {
                            if (txt == "已关注") {
                                dynamicfollow.removeClass("f-white").removeClass("bg-green").addClass("f-gray").addClass("b-green");
                                dynamicfollow.text("关注");
                            }
                            else {
                                dynamicfollow.addClass("f-white").addClass("bg-green").removeClass("f-gray").removeClass("b-green");
                                dynamicfollow.text("已关注");
                            }
                        }
                    })

                    params.onFollowSuccess && typeof params.onFollowSuccess == "function" && params.onFollowSuccess(item);
                }).catch(res => {
                    $.toast(res.msg || res, 'forbidden');
                })
            });
            //点击图片
            params.list.forEach(a => {
                jQuery("#imglist" + a.DynamicId).FlyZommImg({
                    rollSpeed: 100,//切换速度
                    miscellaneous: false,//是否显示底部辅助按钮
                    closeBtn: true,//是否打开右上角关闭按钮
                    hideClass: 'hideImg',//不需要显示预览的 class
                    imgQuality: 'original',//图片质量类型  thumb 缩略图  original 默认原图
                    slitherCallback: function (direction, DOM) {//左滑动回调 两个参数 第一个动向 'left,firstClick,close' 第二个 当前操作DOM 
                        // 为了一开始居中显示
                        $('.fly-zoom-box-img').css('width', '100%').css('height', 'auto').css('top', 0).css('bottom', 0).css('margin', 'auto');
                    }
                });
            })

            //点赞
            $(".lobster_dynamic_like").click(function (event) {
                event.stopPropagation();
                var _this = $(this);
                var islike = _this.data("islike");
                var item = _this.parents(".dynamicitem").data("item");
                lobsterh5.POST("/lowcode/APP20211203093514696/name/?fun=Dynamic.LikeDynamic", {
                    DynamicId: item.DynamicId,
                    TopicId: item.TopicId,
                    BloggerId: item.BloggerId,
                    IsLike: islike == 1 ? 0 : 1,
                    UserId: params.userId,
                }).then(res => {
                    $.toast(islike == 1 ? '取消成功' : '点赞成功', 'text');
                    if (islike == 1) {
                        _this.removeClass("icon-38").addClass("icon-65");
                        _this.data("islike", 0);
                    }
                    else {
                        _this.addClass("icon-38").removeClass("icon-65");
                        _this.data("islike", 1);
                    }
                    _this.next("span").text(res.dynamic.LikesCount)
                    params.onLikeSuccess && typeof params.onLikeSuccess == "function" && params.onLikeSuccess(item);
                }).catch(res => {
                    $.toast(res.msg || res, 'forbidden');
                })
            });
            //评论
            $(".lobster_dynamic_evaluate").click(function (event) {
                event.stopPropagation();
                var _this = $(this);
                var item = _this.parents(".dynamicitem").data("item");
                if (params.detail) {
                    params.onEvaluateSuccess && typeof params.onEvaluateSuccess == "function" && params.onEvaluateSuccess(item);
                } else {
                    var item = $(this).parents(".dynamicitem").data("item");
                    location.href = "/weuiapp/3079/dynamic/detail.html?dynamicId=" + item.DynamicId + "&evalute=1";
                }
            })
            //收藏
            $(".lobster_dynamic_collect").click(function (event) {
                event.stopPropagation();
                var _this = $(this);
                var iscollection = _this.data("iscollection");
                var item = _this.parents(".dynamicitem").data("item");
                lobsterh5.POST("/lowcode/APP20211203093514696/name/?fun=Dynamic.CollectDynamic", {
                    DynamicId: item.DynamicId,
                    IsCollection: iscollection == 1 ? 0 : 1,
                    UserId: params.userId
                }).then(res => {
                    $.toast(iscollection == 1 ? '取消成功' : '收藏成功', 'text');
                    if (iscollection == 1) {
                        _this.removeClass("icon-48").addClass("icon-49");
                        _this.data("iscollection", 0);
                    }
                    else {
                        _this.addClass("icon-48").removeClass("icon-49");
                        _this.data("iscollection", 1);
                    }
                    params.onCollectSuccess && typeof params.onCollectSuccess == "function" && params.onCollectSuccess(item);
                }).catch(res => {
                    $.toast(res.msg || res, 'forbidden');
                })
            });
            //举报
            $(".lobster_dynamic_report").click(function (event) {
                event.stopPropagation();
                var item = $(this).parents(".dynamicitem").data("item");
                location.href = "/weuiapp/3079/dynamic/report.html?dynamicId=" + item.DynamicId;
            })
            //删除
            $(".lobster_dynamic_delete").click(function (event) {
                event.stopPropagation();
                var _this = $(this);
                var item = $(this).parents(".dynamicitem").data("item");
                $.confirm("提示", "确定删除吗？", function () {
                    lobsterh5.POST("/lowcode/APP20211203093514696/name/?fun=Dynamic.DeleteDynamic", {
                        DynamicId: item.DynamicId,
                        UserId: params.userId,
                    }).then(res => {
                        _this.parents(".dynamicitem").fadeOut("normal");
                        params.onDeleteSuccess && typeof params.onDeleteSuccess == "function" && params.onDeleteSuccess(item);
                    }).catch(res => {
                        $.toast(res.msg || res, 'forbidden');
                    })
                }, function () {
                    //取消操作
                });
            })
            //主题
            $(".lobster_dynamic_topic").click(function (event) {
                event.stopPropagation();
                var item = $(this).parents(".dynamicitem").data("item");
                location.href = "/weuiapp/3079/dynamic/topicdetail.html?topicId=" + item.TopicId;
            })
        })
    };
}($);